package com.gadgets.busi.bm06.controller;

import com.gadgets.busi.bm06.dto.TradeInfoAddDTO;
import com.gadgets.busi.bm06.dto.TradeLinkInfoAddDTO;
import com.gadgets.busi.bm06.dto.TradeNodeQueryDTO;
import com.gadgets.busi.bm06.pojo.TradeNodeInfo;
import com.gadgets.busi.bm06.pojo.TradeNodeLink;
import com.gadgets.busi.bm06.service.ITradeInfoSerivce;
import com.gadgets.framework.common.dto.HttpBaseResp;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.util.CacheUtils;
import com.gadgets.framework.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2022-02-27
 */
@RestController
@RequestMapping(value = "/EU4")
public class TradeInfoControllerImpl {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    @Qualifier(value = "tradeInfoServiceImpl")
    private ITradeInfoSerivce tradeInfoSerivce;
    @Autowired
    private CacheUtils cacheUtils;

    private HashMap<Long,  List<TradeNodeLink>> linkHashMap = null;
    private HashMap<Long, TradeNodeInfo> nodeHashMap = null;

    /**
     * 新增贸易节点信息
     * @param request                       网络请求信息
     * @param response                      网络应答信息
     * @param addDTO                        新增信息
     * @return                              新增结果
     * @throws GadgetsServiceException      异常信息
     */
    @RequestMapping(value = "/addTradeInfo.do")
    @SuppressWarnings("rawtypes")
    public HttpBaseResp addTradeNodeInfo(HttpServletRequest request, HttpServletResponse response, TradeInfoAddDTO addDTO) throws GadgetsServiceException{
        tradeInfoSerivce.addTradeNodeInfo(addDTO);
        return new HttpBaseResp();
    }

    /**
     * 新增贸易节点连接信息
     * @param request                       网络请求信息
     * @param response                      网络应答信息
     * @param tradeNodeLink                 贸易连接信息
     * @return                              新增结果
     * @throws GadgetsServiceException      异常信息
     */
    @RequestMapping(value = "/addTradeLink.do")
    public HttpBaseResp<String> addTradeNodeLink(HttpServletRequest request, HttpServletResponse response, TradeLinkInfoAddDTO tradeNodeLink) throws GadgetsServiceException{
        int i = 0;
        Long[] nextNodeIds = StringUtils.splitStrToLong(tradeNodeLink.getNextNodeIds());
        for (Long nextNodeId : nextNodeIds) {
            TradeNodeLink nodeLink = new TradeNodeLink();
            nodeLink.setNodeId(tradeNodeLink.getNodeId());
            nodeLink.setLinkNodeId(nextNodeId);
            try {
                tradeInfoSerivce.addTradeLinkInfo(nodeLink);
            } catch (GadgetsServiceException e) {
                logger.error("输出节点{}，输入节点{}已存在",new Object[]{nodeLink.getNodeId(), nodeLink.getLinkNodeId()});
                i++;
            }
        }
        HttpBaseResp<String> resp = new HttpBaseResp<>();
        resp.setErrorMsg("计划新增记录"+nextNodeIds.length+"条，失败"+i+"条");
        return resp;
    }

    /**
     * 查询贸易节点信息
     * @param request                   网络请求
     * @param response                  网络应答
     * @param queryDTO                  查询条件
     * @param pagination                分页条件
     * @return                          查询结果
     * @throws GadgetsServiceException  异常信息
     */
    @RequestMapping(value = "/queryTradeNodeInfo.do")
    public HttpBaseResp<TradeNodeInfo> selectNodeInfo(HttpServletRequest request, HttpServletResponse response, TradeNodeQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException{
        return new HttpBaseResp<TradeNodeInfo>(tradeInfoSerivce.selectTradeNodeInfo(queryDTO, pagination), pagination);
    }

}
